// manager/pages/poster/index.js
import config from '../../../utils/config';
import { request, uploadFile } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      	baseUrl: config.baseUrl,
		currentIndex: 0,
		posterType: "",
		posterTypes: [],
		posterList: [],
		myGeneratedPosterList: [],
        // 显示上传模板弹窗
        showCropper: false,
        // 显示海报弹窗
        showPosterPopup: false,
        currentMyPoster: {
            posterTitle: "",
            posterSrc: "",
		    posterDetails: "",
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
		this.getPosterType();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

	},
	
	/** 获取海报分类 */
    getPosterType(e) {
		let posterTypes = [
			{
				id: 0,
				dictLabel: "全部",
				dictValue: ""
			}
		];
		// 通用海报
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
				dictType: "ecard_poster_curr_type"
			}
        }).then(res => {
            if(res.code == 0){
				let length = posterTypes.length;
				res.data.forEach((item,index) => {
					posterTypes.push({
						id: length+index,
						dictLabel: item.dictLabel,
						dictValue: item.dictValue
					})
				})
                // 行内海报
				return request({
					url: "/commonApi/getDictionaryInfo",
					method: "GET",
					data: {
						dictType: "ecard_poster_bank_type"
					}
				})
            }
        }).then(res => {
            if(res.code == 0){
				let length = posterTypes.length;
				res.data.forEach((item,index) => {
					posterTypes.push({
						id: length+index,
						dictLabel: item.dictLabel,
						dictValue: item.dictValue
					})
                })
				posterTypes.push({
					id: posterTypes.length,
					dictLabel: "自制模板",
					dictValue: "zizhi"
                })
                posterTypes.push({
					id: posterTypes.length,
					dictLabel: "我的海报",
					dictValue: "wode"
                })
                
				this.setData({
					posterTypes
				})
				this.getPosterList();
			}
        })
    },

	/** 海报分类点击事件*/
    posterTypeHandle(e) {
		let { item,index } = e.currentTarget.dataset;
        this.setData({
			currentIndex: index,
			posterType: item.dictValue,
		})
        if(item.dictValue == "zizhi"){
            this.getMyMakePosterList();
            return
        }
        if(item.dictValue == "wode"){
            this.getMyGeneratedPosterList();
            return
		}
		if(this.data.myGeneratedPosterList.length>0){
			this.setData({
				myGeneratedPosterList: []
			})
		}
        this.getPosterList();
	},
	
	/** 根据posterType获取海报列表 */
    getPosterList(e) {
        request({
            url: "/poster/getOrganPosterList",
            method: "GET",
            data: this.data.posterType ? { posterType: this.data.posterType } : {}
        }).then(res => {
            if(res.code == 0){
				this.setData({
					posterList: res.data
				})
            }
        })
	},

	/** 获取自制模板海报列表 */
    getMyMakePosterList(callback) {
        request({
            url: "/poster/getMyMakePosterList",
            method: "GET",
        }).then(res => {
            if(res.code == 0){
				this.setData({
					posterList: res.data
                })
                callback&&callback();
            }
        })
    },

    /** 获取我的海报列表 */
    getMyGeneratedPosterList() {
        request({
            url: "/poster/getMyGeneratedPosterList",
            method: "GET",
        }).then(res => {
            if(res.code == 0){
				this.setData({
					myGeneratedPosterList: res.data
                })
            }
        })
    },
    /** 跳转编辑海报*/
    toEditPoster(e) {
        let { item } = e.currentTarget.dataset;
        if(this.data.posterType == "zizhi"){
            wx.navigateTo({
                url: `/manager/pages/editposter/index?posterType=${this.data.posterType}&posterId=${item.id}&title=自制模板`,
            })
            return
        }
        if(this.data.posterType == "wode"){
            let { currentMyPoster } = this.data;
            currentMyPoster.posterSrc = item.imgFile;
            currentMyPoster.posterDetails = item.details;
            this.setData({
                showPosterPopup: true,
                currentMyPoster
            })
            return
        }
        wx.navigateTo({
            url: `/manager/pages/editposter/index?${this.data.posterType?'posterType='+this.data.posterType+'&':''}posterId=${item.id}`,
        })
    },

    /** 显示上传自制模板弹窗 */
    uploadTemplate(e){
        console.log(e)
        this.setData({
            showCropper: true
        })
    },
    /** 关闭上传自制模板弹窗 */
    cropperClose(e){
        this.setData({
            showCropper: false
        })
    },
    /** 上传自制模板 */
    cropperUpload(e){
        console.log(e)
        uploadFile({
            url: "/poster/addMyMakePoster",
            filePath: e.detail.url,
            name: "imageFile"
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    showCropper: false
                })
                this.getMyMakePosterList(() => {
                    wx.showToast({
                        title: "上传成功！",
                        icon: 'none',
                        duration: 2000
                    });
                });
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /** 删除自制模板 */
    deleteTemplate(e){
        let { item } = e.currentTarget.dataset;
        let that = this;
        wx.showModal({
            title: '提示',
            content: '是否删除该自制模板？',
            success (res) {
                if (res.confirm) {
                    request({
                        url: "/poster/delMyMakePoster",
                        method: "GET",
                        data: {
                            posterId: item.id,
                            imgUrl: item.imgFile,
                        }
                    }).then(res => {
                        if(res.code == 0){
                            that.getMyMakePosterList(() => {
                                wx.showToast({
                                    title: "删除成功！",
                                    icon: 'none',
                                    duration: 2000
                                });
                            });
                        }
                    })
                    // console.log('用户点击确定')
                } else if (res.cancel) {
                    // console.log('用户点击取消')
                }
            }
        })
    },

    /** 关闭海报弹窗 */
	posterClose(e){
        this.setData({
            showPosterPopup: false
        })
    },
	


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
})